#include "mytoolbar.h"
#include <QAction>
#include <QDebug>
#include <QPushButton>
#include <QFileDialog>
#include <QtGlobal>
#include <QTime>
#include <QLabel>
#include <QDialog>
int myToolBar::i=0;
myToolBar::myToolBar(QToolBar *parent) : QToolBar(parent)
{
    a0 = new QAction();
    a0->setText("钉住");
    a1 = new QAction();
    a1->setIcon(QIcon(":/images/homeWork.jpg"));
    a1->setText("作业");
    a2 = new QAction();
    a2->setIcon(QIcon(":/images/hand.jpg"));
    a2->setText("举手");
    a3 = new QAction();

    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    if(qrand()%10<5){
        a3->setIcon(QIcon(":/images/noline.png"));
        a3->setText("离线");
    }else{
        a3->setIcon(QIcon(":/images/online.png"));
        a3->setText("在线");
    }
    a4 = new QAction();
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    if(qrand()%10<5){
        a4->setIcon(QIcon(":/images/nomessage.png"));
        a4->setText("无消息");
    }else{
        a4->setIcon(QIcon(":/images/message.png"));
        a4->setText("有消息");
    }
    connect(a4,&QAction::triggered,[=](){
        w1->setWindowFlags(Qt::Dialog);
        w1->show();
    });
    a5 = new QAction();
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    if(qrand()%10<5){
        a5->setIcon(QIcon(":/images/nofile.jpg"));
        a5->setText("下载文件");
    }else{
        a5->setIcon(QIcon(":/images/file.jpg"));
        a5->setText("下载文件");
    }
    a6 = new QAction();
    a6->setIcon(QIcon(":/images/help.jpg"));
    a6->setText("帮助");


    this->addAction(a0);

    this->addAction(a3);
    this->addAction(a2);
    this->addAction(a4);
    this->addAction(a1);
    this->addAction(a5);
    this->addAction(a6);

    connect(a0,&QAction::triggered,[=](){
        if(++myToolBar::i%2){
            a0->setText("未钉住");
        }else{
            a0->setText("钉住");
        }

    });
    connect(a2,&QAction::triggered,this,&myToolBar::on_a2_clicked);
    connect(a1,&QAction::triggered,this,&myToolBar::on_a1_clicked);

}
void myToolBar::on_a2_clicked(){
    static int i_a2 = 1;
    switch (++i_a2 % 2) {
        case(1) :
            a2->setText("举手");
            a2->setIcon(QIcon(":/images/hand.jpg"));
            break;
        case(0) :
            a2->setText("放下");
            a2->setIcon(QIcon(":/images/handRecall.jpg"));
        break;
    }
}
void myToolBar::on_a1_clicked(){
    //定义文件对话框类
    QFileDialog *fileDialog = new QFileDialog(this);
    //定义文件对话框标题
    fileDialog->setWindowTitle(tr("选择要提交的作业文件"));
    //设置默认文件路径
    fileDialog->setDirectory("\\/");
    //设置文件过滤器
    fileDialog->setNameFilter(tr("all(*.*);;文本(*.txt);;可执行文件(*.exe)"));
    fileDialog->setFileMode(QFileDialog::ExistingFiles);
    //设置视图模式
    fileDialog->setViewMode(QFileDialog::Detail);
    //打印所有选择的文件的路径
    QStringList fileNames;
    if(fileDialog->exec())
    {
       fileNames = fileDialog->selectedFiles();
    }
    for(auto tmp:fileNames)
       qDebug()<<tmp<<endl;
}
void myToolBar::enterEvent(QEvent *)
{
    this->show();
}
void myToolBar::leaveEvent(QEvent *)
{
    if(myToolBar::i == 1){
           this->hide();
    }

}
